/**
 *    Copyright 2012 Geensoft S.A.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **/
package ar.com.greensoft.neo.web.jsf.beans.admin.groups;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import ar.com.greensoft.neo.dao.DaoException;
import ar.com.greensoft.neo.dao.GroupDao;
import ar.com.greensoft.neo.model.Group;
import ar.com.greensoft.neo.model.GroupAuthorities;
import ar.com.greensoft.neo.util.ApplicationRoles;
import ar.com.greensoft.neo.web.jsf.beans.common.CommonViewBean;
import ar.com.greensoft.neo.web.jsf.util.JSFMenuUtil;

/**
 * @author Jorge E. Villaverde
 *
 */
@Component
@RequestScoped
@ManagedBean
public class CreateGroupBean extends CommonViewBean {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@NotNull
	@Size(max = 255)
	private String name;
	
	@Autowired
	private GroupDao groupDao;
	
	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return this.name;
	}

	private List<String> authorities;
	
	private List<String> selectedAuthorities;
	
	@PostConstruct  
	public void init() { 
		authorities = new ArrayList<String>();
		for(ApplicationRoles role : ApplicationRoles.values())
			authorities.add(role.name());
		
		clearData();  
	} 

	public void clearData() {
		this.selectedAuthorities = null;
	}

	@Override
	protected String menuRole() {
		return ADMIN_PATH;
	}

	@Override
	protected String menuName() {
		return "groups";
	}
	
	public List<String> getAuthorities() {
		return authorities;
	}

	public void setAuthorities(List<String> authorities) {
		this.authorities = authorities;
	}

	public List<String> getSelectedAuthorities() {
		return selectedAuthorities;
	}

	public void setSelectedAuthorities(List<String> selectedAuthorities) {
		this.selectedAuthorities = selectedAuthorities;
	}
	
	public String createGroup(){
		if(logger.isTraceEnabled())
			logger.trace("Creating Group: " + name);
		
		Group group = new Group();
		group.setName(name);
		try {
			groupDao.persist(group);

			if(group != null){
				for(String authorityName : selectedAuthorities){
					GroupAuthorities ga = new GroupAuthorities();
					ga.setGroup(group);
					ga.setAuthority(authorityName);
					groupDao.addAuthority(ga);
					group.getAuthorities().add(ga);
				}
			}
		} catch (DaoException e) {
			logger.trace("Group " + name + " CREATED.");
		}
		
		if(logger.isTraceEnabled())
			logger.trace("User " + name + " CREATED.");
		
		if(persistReturnView != null && !persistReturnView.isEmpty())
			return persistReturnView + JSFMenuUtil.JSF_REDIRECT;
		return showCreate();
	}	
}
